<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="/css/index.css">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0-beta3/css/all.min.css"> <!-- Font Awesome -->
    <title>{{.title}}</title>
    <script>
        document.addEventListener('DOMContentLoaded', function() {
            var modal = document.getElementById("deleteModal");
            var closeModal = document.getElementsByClassName("close")[0];
            var confirmDeleteBtn = document.getElementById("confirmDelete");
            var cancelBtn = document.getElementsByClassName("cancel")[0];
            var modalMessage = document.getElementById("modalMessage");
            var currentRow = null;

            function openModal(message, showCancel = true) {
                modalMessage.textContent = message;
                modal.style.display = "block";
                if (!showCancel) {
                    cancelBtn.style.display = "none";
                } else {
                    cancelBtn.style.display = "inline-block";
                }
            }

            function closeModalFunction() {
                modal.style.display = "none";
                confirmDeleteBtn.onclick = null;
            }

            function deleteFavorite(favoriteId, row) {
                fetch('/favorite/delete', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json',
                    },
                    body: JSON.stringify({ id: favoriteId }),
                })
                .then(response => {
                    if (!response.ok) {
                        throw new Error('网络响应不正常');
                    }
                    row.remove();
                    openModal("收藏项已成功删除！", false);
                    confirmDeleteBtn.onclick = function() {
                        closeModalFunction();
                        location.reload(); // 添加这一行来刷新页面
                    };
                })
                .catch(error => {
                    console.error('请求过程中出现问题:', error);
                    openModal("删除收藏项失败，请重试！");
                });
            }

            var deleteButtons = document.querySelectorAll('.delete-button');
            deleteButtons.forEach(button => {
                button.addEventListener('click', function(event) {
                    event.preventDefault();
                    currentRow = button.parentElement.parentElement;
                    var favoriteId = parseInt(currentRow.children[1].innerText);

                    openModal("确认删除该收藏项吗？");

                    confirmDeleteBtn.onclick = function() {
                        deleteFavorite(favoriteId, currentRow);
                    };
                });
            });

            closeModal.onclick = function() {
                closeModalFunction();
            };

            cancelBtn.onclick = function() {
                closeModalFunction();
            };

            window.onclick = function(event) {
                if (event.target == modal) {
                    closeModalFunction();
                }
            };
        });
    </script>
</head>
<body class="container">
<header>
    <h1 class="title">{{.title}}</h1>
</header>
<main>
    <section class="students-table">
        <table>
            <thead>
            <tr>
                <th>序号</th>
                <th>学号</th>
                <th>姓名</th>
                <th>语文</th>
                <th>数学</th>
                <th>英语</th>
                <th>操作</th>
            </tr>
            </thead>
            <tbody>
            {{range $index, $value := .favorites}}
            <tr>
                <td>{{inc $index}}</td>
                <td>{{$value.Id}}</td>
                <td>{{$value.Name}}</td>
                <td>{{$value.Chinese}}</td>
                <td>{{$value.Math}}</td>
                <td>{{$value.English}}</td>
                <td>
                    <button class="delete-button" onclick="deleteStudent(this)">
                        <i class="fas fa-trash-alt"></i> <!-- 垃圾桶图标 -->
                    </button>
                </td>
            </tr>
            {{end}}
            </tbody>
        </table>
    </section>
    <div class="actions">
        <a href="/index">返回首页</a>
    </div>

    <!-- Delete Modal -->
    <div id="deleteModal" class="modal">
        <div class="modal-content">
            <div class="modal-header">
                <span class="close">&times;</span>
            </div>
            <div class="modal-body">
                <p id="modalMessage"></p>
            </div>
            <div class="modal-footer">
                <button class="cancel">取消</button>
                <button class="confirm" id="confirmDelete">确认</button>
            </div>
        </div>
    </div>
</main>
</body>
</html>
